/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowki, Maciej Gajewski             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// libc
#include <stdio.h>

// Qt
#include <QCoreApplication>
#include <QThread>
#include <QTcpServer>
#include <QTcpSocket>

// local
#include "gexception.h"
#include "serverlistener.h"
#include "imageanalyzer.h"

// globals
quint16 	port	= 5102;							///< tcp port

// =========================== main server thread ===========
class MainServerThread : public QThread
{
public:
	MainServerThread(){}
	virtual ~MainServerThread(){}
	
	virtual void run()
	{
		try
		{
			// create server listener
			ServerListener server;
	
			// listen, process
			qDebug("Starting Autostop server: %s %s", __DATE__, __TIME__ );
			server.listen( port );
		}
		catch( GException& e )
		{
			qDebug("server error: %s", (const char*)e.getMessage().toAscii() );
		}
	}
};


// ---------------------------- usage ----------------------------
void usage()
{
	fprintf( stderr,
		"Autostop server, listens on TCP port %d, on all interfaces\n"
		, (int)port
		);
}

// ---------------------------- run ----------------------------
int main( int argc, char** argv )
{
	QCoreApplication app( argc, argv );
	
	try
	{
		MainServerThread	server;
		ImageAnalyzer		analyzer;
		
		// start server in backgorund
		server.start();
		
		// start analyzer in foreground
		analyzer.run();
	}
	catch ( const GException& e )
	{
		qDebug("Error: %s", qPrintable( e.getMessage() ) );
	}
	catch( ... )
	{
		qDebug("unknown error");
	}
	return 0;
}



// eof

